package com.sz.sidi.dao.hibernate.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sz.sidi.dao.hibernate.BaseHbDao;
import com.sz.sidi.entity.TuserInfo;
import com.sz.sidi.exceptions.DaoException;
import com.sz.sidi.util.GlobalFun;

public class BaseHbDaoimpl extends HibernateDaoSupport implements BaseHbDao{
	protected Logger logger = Logger.getLogger(BaseHbDaoimpl.class);
	
	public List<TuserInfo> getHbUserInfo() {
		List<TuserInfo> userinfo = this.getHibernateTemplate().find(" from TuserInfo");
		return userinfo;
	}

	public boolean insert(Object o) throws DaoException {
		try {
			this.getHibernateTemplate().save(o);
			return true;
		}catch(Exception e){
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public boolean delete(Object o) throws DaoException {
		try {
			this.getHibernateTemplate().delete(o);
			return true;
		} catch (Exception e) { // ��׽����
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}
	public void deleteAll(List obj)throws DaoException{
		try {
			this.getHibernateTemplate().deleteAll(obj);
		} catch (Exception e) { // ��׽����
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public boolean edit(Object o) throws DaoException {
		try {
			this.getHibernateTemplate().update(o);
			return true;
		} catch (Exception e) { // ��׽����
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public List find(String hql) throws DaoException {
		List result = null;
		try {
			result = this.getHibernateTemplate().find(hql);
			return result;
		} catch (Exception e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public Object save(Object obj) throws DaoException {
		try{
			this.getHibernateTemplate().save(obj);
		}catch (Exception e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
		return obj;
	}
	public boolean saveOrUpdate(Object obj) throws DaoException {
		try{
			this.getHibernateTemplate().saveOrUpdate(obj);
		}catch (Exception e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
		return true;
	}
	public boolean merge(Object obj) throws DaoException{
		try{
			this.getHibernateTemplate().merge(obj);
		}catch (Exception e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
		return true;
	}
	public boolean mergeAll(List objList) throws DaoException{
		if(objList==null)
			return false;
		try{
			for(Object obj:objList){
				this.getHibernateTemplate().merge(obj);
			}
		}catch (Exception e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
		return true;
	}
	public boolean saveOrUpdateAll(List list) throws DaoException{
		try{
			this.getHibernateTemplate().saveOrUpdateAll(list);
		}catch (Exception e) {
			e.printStackTrace();
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
		return true;
	}

	public List getAllEntity(Class objectClass) throws DaoException {
		try {
			return this.getHibernateTemplate().find(" from "+objectClass.getName()+" obj ");
		} catch (HibernateException e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public Object getEntity(Class objectClass, Serializable key) throws DaoException {
		try {
			return this.getHibernateTemplate().get(objectClass, key);
		} catch (HibernateException e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}

	public Object getEntity(String objectName, Serializable key) throws DaoException {

		try {
			return this.getHibernateTemplate().get(objectName, key);
		} catch (HibernateException e) {
			throw new DaoException(GlobalFun.getErrCauseMsg(e));
		}
	}
	
	public Object findEntityByKey(Class objClass, Serializable keyObj)
			throws DaoException {
		try {
			return this.getHibernateTemplate().get(objClass, keyObj);
		} catch (DataAccessException e) {
			logger.debug("method:<findEntityByKey> " + e.getMessage());
			throw new DaoException("Dao��ͨ����������ȡʵ��ʱ���");
		} catch (Exception e) {
			logger.debug("method:<findEntityByKey> " + e.getMessage());
			throw new DaoException("Dao��ͨ����������ȡʵ��ʱ���");
		}

	}

	public void evictEntityModel(Object obj) throws DaoException {
		this.getHibernateTemplate().evict(obj);
	}

	public boolean updateObjByOperFlag(int operFlag, Object obj)
			throws DaoException {
		boolean result = false;
		try {
			switch (operFlag) {
			case 0: //新增或修改������޸�
				this.getHibernateTemplate().saveOrUpdate(obj);
				result = true;
				break;
			case 1: //新增����
				result = insert(obj); 
				break;
			case 2: //修改�޸�
				result = edit(obj); 
				break;
			case 3: //删除
				delete(obj);
				result = true;
				break;
			}
		} catch (Exception e) {
			throw new DaoException(e.getMessage(), e);
		}
		return result;
	}

	public boolean updateObjByOperFlag(int operFlag, Object obj, Serializable id)
			throws DaoException {
		boolean result = false;
		try {
			switch (operFlag) {
			case 0: //新增或修改
				this.getHibernateTemplate().saveOrUpdate(obj);
				result = true;
				break;
			case 1: //新增
				result = insert(obj);
				break;
			case 2: //修改�޸� 
				result = edit(obj);
				break;
			case 3: //删除
				obj= this.getHibernateTemplate().load(obj.getClass(), id);
				result = delete(obj);
				break;
			}
		} catch (Exception e) {
			throw new DaoException(e.getMessage(), e);
		}
		return result;		
	}
 
	public List findByExample(Object obj,Serializable id)throws DaoException{ 
		try {
			if (null == id){
				return this.getHibernateTemplate().findByExample(obj);
			}else{
				List list = new ArrayList(1);
				list.add(this.findEntityByKey(obj.getClass(), id));
				return list;
			}
			
		} catch (DataAccessException e) {
			logger.error("method:<findByExample> " + e.getMessage());
			throw new DaoException("e.getMessage()");
		} catch (Exception e) {
			logger.error("method:<findEntityByKey> " + e.getMessage());
			throw new DaoException("e.getMessage()");
		} 
	} 
	public void clearSession()throws DaoException{
		this.getHibernateTemplate().clear();
	}
}
